/*
 * @(#)MaintUserRoleAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SqlAccountUtility;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.web.AbstractActionForm;

/**
  MaintWorkflowGroupAction.java

  This class is for maint user role.

    @author          Angus Shiu
    @company         DCIVision Limited
    @creation date   29/07/2003
    @version         $Revision: 1.9 $
*/

public class MaintWorkflowGroupAction extends com.dcivision.setup.web.AbstractMaintPermissionAction {

  public static final String REVISION = "$Revision: 1.9 $";


  /** Creates a new instance of MaintWorkflowGroupAction */
  public MaintWorkflowGroupAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowGroupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_GROUP);
  }

  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintWorkflowGroupForm workflowGroupForm = (MaintWorkflowGroupForm)form;
    workflowGroupForm.setGroupName(null);
  }

  /**
   * getObjectType
   *
   * @return  The corresponding object type of permission setup action.
   */
  public String getObjectType() {
    return(GlobalConstant.OBJECT_TYPE_WORKFLOW);
  }
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
	  MaintWorkflowGroupForm maintWorkflowGroupForm = (MaintWorkflowGroupForm)form;
	  if(GlobalConstant.OP_MODE_DELETE.equals(maintWorkflowGroupForm.getOpMode())) {
		  if(this.isContainerWorkflow(request)) {
			  addError(request, "errors.workflow.isContainerWf");
			  maintWorkflowGroupForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
			  return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
		  }		  
	  }
	  return super.execute(mapping, form, request, response);
  }
  
  /**
   * the mother return a value that use judge WorkflowRecord,if container workflow.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   */
  public boolean isContainerWorkflow(HttpServletRequest request) {
    boolean result = false;
      String groupID = request.getParameter("ID");
      String workflowcondition = " WORKFLOW_GROUP_ID=" + ( new Integer(groupID).intValue() ) + " AND RECORD_STATUS='" + GlobalConstant.RECORD_STATUS_ACTIVE + "' ";
      int workflowCountID = SqlAccountUtility.getIDCountFromSql(this.getConnection(request), "workflow_record", workflowcondition);
      if (workflowCountID == 0) {
        result = false;
      } else {
        result = true;
      }   
    return result;
  }

}
